package com.example.demorun2.myHosptional;

import java.util.concurrent.locks.Lock;

public class Person implements Runnable{
    private HospitalWindows hospitalWindows;

    private String name;

    private Boolean check = false;

    public Person(String name,HospitalWindows hospitalWindows){
        this.name = name;
        this.hospitalWindows = hospitalWindows;
    }

    @Override
    public void run() {
        hospitalWindows.addPerson(this);
        if(hospitalWindows.getCheck()){
            hospitalWindows.setCheck();
            System.out.println(name+"开始检查");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(name+"检查结束");
            hospitalWindows.removePerson(this);
        }else{
            System.out.println(name+" 排队等待检查");
            //循环去查看医生谈话标识是否放开
            while (true){
                try {
                    //停顿一下去循环，让cpu有切换时间
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //如果有睡眠，则中断线程信号，会被重新置为为false,所以这里还要再次设置中断标记
                    Thread.currentThread().interrupt();
                }

                //线程判断中断信号
                //有两种中断，一种 isTerrupted，只是判断标记
                //一种 是  interrupted 判断并清除标记 设为false
                if(Thread.interrupted()){
                    break;
                }
                //获取标识
                hospitalWindows.getWindowsCheck();

            }
        }

    }

    public Boolean getCheck(){
        return this.check;
    }

    public void setCheck(Boolean flag){
        this.check = flag;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
